home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / GMenuSimpleEditor / menutreemodel.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  4.6 KB  |  143 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import os.path as os
  6. import gtk
  7. import gtk.gdk as gtk
  8. import gmenu
  9.  
  10. def lookup_system_menu_file(menu_file):
  11.     conf_dirs = None
  12.     if os.environ.has_key('XDG_CONFIG_DIRS'):
  13.         conf_dirs = os.environ['XDG_CONFIG_DIRS']
  14.     
  15.     if not conf_dirs:
  16.         conf_dirs = '/etc/xdg'
  17.     
  18.     for conf_dir in conf_dirs.split(':'):
  19.         menu_file_path = os.path.join(conf_dir, 'menus', menu_file)
  20.         if os.path.isfile(menu_file_path):
  21.             return menu_file_path
  22.     
  23.  
  24.  
  25. def load_icon_from_path(icon_path):
  26.     if os.path.isfile(icon_path):
  27.         
  28.         try:
  29.             return gtk.gdk.pixbuf_new_from_file_at_size(icon_path, 24, 24)
  30.  
  31.     
  32.  
  33.  
  34. def load_icon_from_data_dirs(icon_value):
  35.     data_dirs = None
  36.     if os.environ.has_key('XDG_DATA_DIRS'):
  37.         data_dirs = os.environ['XDG_DATA_DIRS']
  38.     
  39.     if not data_dirs:
  40.         data_dirs = '/usr/local/share/:/usr/share/'
  41.     
  42.     for data_dir in data_dirs.split(':'):
  43.         retval = load_icon_from_path(os.path.join(data_dir, 'pixmaps', icon_value))
  44.         if retval:
  45.             return retval
  46.         retval = load_icon_from_path(os.path.join(data_dir, 'icons', icon_value))
  47.         if retval:
  48.             return retval
  49.     
  50.  
  51.  
  52. def load_icon(icon_theme, icon_value):
  53.     if not icon_value:
  54.         return None
  55.     if icon_name.endswith('.png'):
  56.         icon_name = icon_name[:-len('.png')]
  57.     elif icon_name.endswith('.xpm'):
  58.         icon_name = icon_name[:-len('.xpm')]
  59.     elif icon_name.endswith('.svg'):
  60.         icon_name = icon_name[:-len('.svg')]
  61.     
  62.     
  63.     try:
  64.         return icon_theme.load_icon(icon_name, 24, 0)
  65.     except:
  66.         return load_icon_from_data_dirs(icon_value)
  67.  
  68.  
  69.  
  70. class MenuTreeModel(gtk.TreeStore):
  71.     (COLUMN_IS_ENTRY, COLUMN_ID, COLUMN_NAME, COLUMN_ICON, COLUMN_MENU_FILE, COLUMN_SYSTEM_VISIBLE, COLUMN_USER_VISIBLE) = range(7)
  72.     
  73.     def __init__(self, menu_files):
  74.         gtk.TreeStore.__init__(self, bool, str, str, gtk.gdk.Pixbuf, str, bool, bool)
  75.         self.entries_list_iter = None
  76.         self.icon_theme = gtk.icon_theme_get_default()
  77.         if len(menu_files) < 1:
  78.             menu_files = [
  79.                 'applications.menu',
  80.                 'settings.menu']
  81.         
  82.         for menu_file in menu_files:
  83.             tree = gmenu.lookup_tree(menu_file, gmenu.FLAGS_INCLUDE_EXCLUDED)
  84.             self._MenuTreeModel__append_directory(tree.root, None, False, menu_file)
  85.             system_file = lookup_system_menu_file(menu_file)
  86.             if system_file:
  87.                 system_tree = gmenu.lookup_tree(system_file, gmenu.FLAGS_INCLUDE_EXCLUDED)
  88.                 self._MenuTreeModel__append_directory(system_tree.root, None, True, menu_file)
  89.                 continue
  90.         
  91.  
  92.     
  93.     def __append_directory(self, directory, parent_iter, system, menu_file):
  94.         if not directory:
  95.             return None
  96.         iter = self.iter_children(parent_iter)
  97.         while iter:
  98.             if self[iter][self.COLUMN_ID] == directory.menu_id:
  99.                 break
  100.             
  101.             iter = self.iter_next(iter)
  102.         if not iter:
  103.             iter = self.append(parent_iter)
  104.             self[iter][self.COLUMN_IS_ENTRY] = False
  105.             self[iter][self.COLUMN_ID] = directory.menu_id
  106.             self[iter][self.COLUMN_NAME] = directory.name
  107.             self[iter][self.COLUMN_ICON] = load_icon(self.icon_theme, directory.icon)
  108.             if menu_file is not None:
  109.                 self[iter][self.COLUMN_MENU_FILE] = menu_file
  110.             
  111.         
  112.         if system:
  113.             self[iter][self.COLUMN_SYSTEM_VISIBLE] = True
  114.         else:
  115.             self[iter][self.COLUMN_USER_VISIBLE] = True
  116.         for child_item in directory.contents:
  117.             if isinstance(child_item, gmenu.Directory):
  118.                 self._MenuTreeModel__append_directory(child_item, iter, system, None)
  119.             
  120.             if not isinstance(child_item, gmenu.Entry):
  121.                 continue
  122.             
  123.             child_iter = self.iter_children(iter)
  124.             while child_iter:
  125.                 if child_item.type == gmenu.TYPE_ENTRY and self[child_iter][self.COLUMN_IS_ENTRY] and self[child_iter][self.COLUMN_ID] == child_item.desktop_file_id:
  126.                     break
  127.                 
  128.                 child_iter = self.iter_next(child_iter)
  129.             if not child_iter:
  130.                 child_iter = self.append(iter)
  131.                 self[child_iter][self.COLUMN_IS_ENTRY] = True
  132.                 self[child_iter][self.COLUMN_ID] = child_item.desktop_file_id
  133.                 self[child_iter][self.COLUMN_NAME] = child_item.name
  134.                 self[child_iter][self.COLUMN_ICON] = load_icon(self.icon_theme, child_item.icon)
  135.             
  136.             if system:
  137.                 self[child_iter][self.COLUMN_SYSTEM_VISIBLE] = not (child_item.is_excluded)
  138.                 continue
  139.             self[child_iter][self.COLUMN_USER_VISIBLE] = not (child_item.is_excluded)
  140.         
  141.  
  142.  
  143.